package sy.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import sy.jackson.SysUnitMixin;
import sy.jackson.SysUserMixin;
import sy.model.SysAccount;
import sy.model.SysBuilding;
import sy.model.SysProject;
import sy.model.SysProjectInformationMapping;
import sy.model.SysProjectOwnerMapping;
import sy.model.SysUnit;
import sy.model.SysUser;
import sy.model.SysUserUnitMapping;
import sy.service.SysAccountServiceI;
import sy.service.SysBuildingServiceI;
import sy.service.SysProjectOwnerMappingServiceI;
import sy.service.SysProjectServiceI;
import sy.service.SysUnitServiceI;
import sy.service.SysUserServiceI;
import sy.service.SysUserUnitMappingServiceI;
import sy.util.MessageObject;

@Controller
@RequestMapping("userController")
@SessionAttributes("currAccount")
public class UserController {
	
	@Autowired
	private SysUserServiceI sysUserService;
	@Autowired
	private SysAccountServiceI sysAccountService;
	@Autowired
	private SysProjectServiceI sysProjectService;
	@Autowired
	private SysUnitServiceI sysUnitService;
	@Autowired
	private SysBuildingServiceI sysBuildingService;
	@Autowired
	private SysUserUnitMappingServiceI sysUserUnitMappingService;
	private static final ObjectMapper objectMapper = new ObjectMapper();

	/************************** view controller ***************************/


	/**
	 * @Description: 首页视图
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月30日 
	 */
	@RequestMapping(value="/view")
	public String objectView(Model model){
		List<SysUser> userList = sysUserService.list();
		
		model.addAttribute("userList",userList);
		return "userInfo/user/user";
	}
	
	/**
	 * @Description: 新增视图
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月30日 
	 */
	@RequestMapping(value="/view/new",method=RequestMethod.GET)
	public String newObjectInfo(Model model){
		
		String uuid = UUID.randomUUID().toString();
		SysUser sysUser = new SysUser();
		sysUser.setId(uuid);
		
		List<SysAccount> accountList = sysAccountService.list();			
		List<SysProject> projectList = sysProjectService.list();
		List<SysBuilding> buildingList = sysBuildingService.list();
		List<SysUnit> unitList = sysUnitService.list();

		model.addAttribute("user",sysUser);
		model.addAttribute("accountList",accountList);
		model.addAttribute("projectList",projectList);
		model.addAttribute("buildingList",buildingList);
		model.addAttribute("unitList",unitList);
		model.addAttribute("type",1);
		
		return "userInfo/user/userFrom";
	}

	/**
	 * @Description: 编辑视图
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月30日 
	 */
	@RequestMapping(value="/view/edit",params="id",method=RequestMethod.GET)
	public String editObjectInfo(@RequestParam String id,Model model){
		
		SysUser sysUser = sysUserService.getSysUserById(id);
		List<SysAccount> accountList = sysAccountService.list();		
		List<SysUnit> unitListCheck = new ArrayList<SysUnit>();
		
		/**
		 * 通过user实体返回中间表，通过中间表构造unit实体
		 */
		List<SysUserUnitMapping> list = sysUserUnitMappingService.listSysUserUnitMappingByKey(sysUser.getId());
		for(SysUserUnitMapping obj : list){
			unitListCheck.add(obj.getSysUnit());
		}
		
		List<SysProject> projectList = sysProjectService.list();
		List<SysBuilding> buildingList = sysBuildingService.list();
		List<SysUnit> unitList = sysUnitService.list();

		model.addAttribute("user",sysUser);
		model.addAttribute("accountList",accountList);
		model.addAttribute("projectList",projectList);
		model.addAttribute("buildingList",buildingList);
		model.addAttribute("unitList",unitList);
		model.addAttribute("unitListCheck",unitListCheck);
		model.addAttribute("type",2);
		
		return "userInfo/user/userFrom";
	}
	
	/**
	 * 
	 * @Title		:获取用户的房址信息
	 * @Description	:TODO
	 * @param		:@param id
	 * @param		:@param model
	 * @param		:@return
	 * @return		:String
	 * @author		:wujiayi
	 * @time		:2017年9月1日 下午6:31:20
	 * @throws
	 */
	@RequestMapping(value="/view/getProjectsByUserId/{id}",method=RequestMethod.GET)
	public String getProjectsByInfoId(@PathVariable String id,Model model){
		
		List<String> addressList = sysUserUnitMappingService.getProjectsByUserId(id);

		model.addAttribute("addressList",addressList);
		return "userInfo/user/ownerProjects";
	}
		
	/************************** data controller ***************************/
	
	/**
	 * @Description: 新增
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月30日 
	 */
	@ResponseBody
	@RequestMapping(value = "/new/{unitIds}", method = RequestMethod.POST,produces="application/json;charset=UTF-8")
	private MessageObject newObjectInfo( @RequestBody SysUser sysUser, Model model, @PathVariable String[] unitIds) {
		MessageObject messageObject = new MessageObject();
		try {
			sysUserService.save(sysUser);//写入User实体信息
			sysUserUnitMappingService.writeIntoUserUnitMapping(sysUser,unitIds);//写入中间表
			
			messageObject.setMessage("添加成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("添加失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	
	/**
	 * @Description: 编辑
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月30日 
	 */
	@ResponseBody
	@RequestMapping(value = "/edit/{unitIds}", method = RequestMethod.POST,produces="application/json;charset=UTF-8")
	private MessageObject editObjectInfo( @RequestBody SysUser sysUser, Model model, @PathVariable ("unitIds") String[] unitIds) {
		MessageObject messageObject = new MessageObject();
		try {
			
			sysUserUnitMappingService.removeByUser(sysUser.getId());
			sysUserUnitMappingService.writeIntoUserUnitMapping(sysUser, unitIds);
			sysUserService.update(sysUser,unitIds);

			messageObject.setMessage("编辑成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("编辑失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	
	/**
	 * @Description: 删除
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月26日 
	 */
	@RequestMapping(value="/del", method=RequestMethod.POST)
	@ResponseBody
	private MessageObject delObjectInfo(@RequestParam("id") String[] ids) {
		MessageObject messageObject = new MessageObject();
		
		try {
			sysUserService.remove(ids);
			messageObject.setMessage("删除成功!");
			messageObject.setStatus(true);
		} catch (Exception e) {
			// TODO: handle exception
			messageObject.setMessage("删除失败!");
			messageObject.setStatus(false);
		}
		return messageObject;
	}
	
	/**
	 * @Description: 返回全部json数据
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月26日 
	 */
	@ResponseBody
	@RequestMapping(value = "/getAllData", method = RequestMethod.POST,produces={"text/html;charset=UTF-8"})
	private String getAllData(HttpServletResponse response) {
		objectMapper.addMixInAnnotations(SysUser.class, SysUserMixin.BasicInfo.class);
		List<SysUser> userList = sysUserService.list();
		try {
			return objectMapper.writeValueAsString(userList);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
}
